/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"

	"github.com/spf13/cobra"
)

func commit(url string, cmd *cobra.Command, args []string) {
	client := http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Printf("创建请求失败: %v\n", err)
		return
	}

	q := req.URL.Query()
	name, _ := cmd.Flags().GetString("name")
	force, _ := cmd.Flags().GetBool("force")
	q.Add("name", name)
	q.Add("force", strconv.FormatBool(force))
	req.URL.RawQuery = q.Encode()

	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("请求失败: %v\n", err)
		return
	}
	defer resp.Body.Close()
	buf, _ := ioutil.ReadAll(resp.Body)
	fmt.Print(string(buf))
}

// commitCmd represents the commit command
var service_commitCmd = &cobra.Command{
	Use: "commit",
	Run: func(cmd *cobra.Command, args []string) {
		commit(cmd_url_prefix+"/api/service/commit", cmd, args)
	},
}

var public_commitCmd = &cobra.Command{
	Use: "commit",
	Run: func(cmd *cobra.Command, args []string) {
		commit(cmd_url_prefix+"/api/public/commit", cmd, args)
	},
}

var package_commitCmd = &cobra.Command{
	Use: "commit",
	Run: func(cmd *cobra.Command, args []string) {
		commit(cmd_url_prefix+"/api/package/commit", cmd, args)
	},
}

func init() {
	serviceCmd.AddCommand(service_commitCmd)
	publicCmd.AddCommand(public_commitCmd)
	packageCmd.AddCommand(package_commitCmd)

	service_commitCmd.Flags().String("name", "", "service name")
	public_commitCmd.Flags().String("name", "", "public name")

	service_commitCmd.Flags().Bool("force", false, "force active")
	public_commitCmd.Flags().Bool("force", false, "force active")
	// service_commitCmd.MarkFlagRequired("name")
	// public_commitCmd.MarkFlagRequired("name")
}
